/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mForms;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author X
 */
class IndexMenu extends Canvas {
    private MainFormss _m;
    private Image _bg;

    private Image _mnu1;
    private Image _mnu2;
    private Image _mnu3;
    private String DKeyPress = "-";
    private int menuSelected = 0;
    private int maxMenu = 3;
    private Image _hover;

    public IndexMenu(MainFormss m) {
        _m = m;
        String lang = _m.lang;

        if(_bg==null)
            try {
                _bg = Image.createImage("/images/bgmenu.png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        if(_hover==null)
            try {
                _hover = Image.createImage("/images/menu/mhover.png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        if(_mnu1==null)
            try {
                _mnu1 = Image.createImage("/images/menu/m1"+lang+".png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        if(_mnu2==null)
            try {
                _mnu2 = Image.createImage("/images/menu/m2"+lang+".png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        if(_mnu3==null)
            try {
                _mnu3 = Image.createImage("/images/menu/m3"+lang+".png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    }

  protected void keyPressed(int keyCode){
    int gameAction = getGameAction(keyCode);

    switch(gameAction){
      case UP:
        DKeyPress = "[UP]";
        break;
      case DOWN:
        DKeyPress = "[DOWN]";
        break;
      case LEFT:
        DKeyPress = "[LEFT]";
        break;
      case RIGHT:
        DKeyPress = "[RIGHT]";
        break;
      case FIRE:
        DKeyPress = "[FIRE]";
        break;
      case GAME_A:
        DKeyPress = "[LEFT_UP]";
        break;
      case GAME_B:
        DKeyPress = "[RIGHT_UP]";
        break;
      case GAME_C:
        DKeyPress = "[LEFT_DOWN]";
        break;
      case GAME_D:
        DKeyPress = "[RIGHT_DOWN]";
        break;
      default:
        DKeyPress = "-";
      break;
    }

    switch(keyCode) {
        case -7:
            //_ownskin.exit();
            //DKeyPress = "[RIGHT_MENU]";
            //DKeyPress = "[RIGHT_MENU]";
            MainFormss.instance().exitMIDlet();
            break;
        case -6:
            DKeyPress = "[LEFT_MENU]";
            break;
    }

    System.out.println(keyCode);
    repaint();
  }

    protected void paint(Graphics g) {
        setFullScreenMode(true);
		g.setColor(0);
//		g1.setColor(0x000D73);
		g.fillRect(0, 0, getWidth(), getHeight());

                if(_bg!=null)
                    g.drawImage(_bg, 0, 0, Graphics.LEFT|Graphics.TOP);

                if(DKeyPress.equals("[DOWN]")) {
                    menuSelected++;
                    if(menuSelected==maxMenu) {
                        menuSelected = 0;
                    }
                } else if(DKeyPress.equals("[UP]")) {
                    menuSelected--;
                    if(menuSelected<0) {
                        menuSelected = maxMenu-1;
                    }
                } else if(DKeyPress.equals("[FIRE]") || DKeyPress.equals("[LEFT_MENU]")) {
                    switch(menuSelected) {
                        case 0:
                            //by surah
                            _m.showPage("surah.txt", null);
                            break;

                        case 1:
                            //by juz
                            _m.showPage("juz.txt", null);
                            break;

                        case 2:
                            //about
                            _m.showPage("about.txt", null);
                            break;
                    }
                }

                g.drawImage(_mnu1, 207, 25, Graphics.TOP|Graphics.LEFT);
                g.drawImage(_mnu2, 207, 70, Graphics.TOP|Graphics.LEFT);
                g.drawImage(_mnu3, 207, 117, Graphics.TOP|Graphics.LEFT);

                switch(menuSelected) {
                    case 0:
                        g.drawImage(_hover, 200, 34, Graphics.TOP|Graphics.RIGHT);
                        break;
                    case 1:
                        g.drawImage(_hover, 200, 79, Graphics.TOP|Graphics.RIGHT);
                        break;
                    default:
                        g.drawImage(_hover, 200, 129, Graphics.TOP|Graphics.RIGHT);
                        break;
                }
    }

}
